package gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import db.domain.Student;
import db.domain.User;
import db.sqldao.StudentSqlDao;
import db.sqldao.UserSqlDao;

/**
 * 用户登录界面
 * 
 * @author 陈千其
 * @date 2011-06-20
 * @version 1.0.0.0
 * 
 */
public class loginForm extends JFrame implements ActionListener, MouseListener {
	private JButton btnLoagin = new JButton("登录");
	private JRadioButton rdbSex[];// 用户类别
	private JPanel jp = new JPanel(null/* new GridLayout(3,2) */);

	private JTextField txtName = new JTextField("admin");
	private JPasswordField txtPassword = new JPasswordField("123456");
	/* 自己添加 */
	private JLabel lblPicture;// 系统图片
	private int width, height, x0, y0, w, ww, h, hh, www, hhh, wwww, x1;

	public Student Student = new Student();
	public User User = new User();

	public loginForm() {
		this.setTitle("用户登录界面");
		width = 350;
		height = 400;
		x0 = 20;
		x1 = 60;
		y0 = 155;
		w = 85;
		ww = 308;
		h = 25;
		hh = 47;
		www = 90;
		hhh = 48;
		wwww = 245;

		txtName.setBounds(x0, y0, ww, h);
		txtPassword.setBounds(x0, y0 + hh + h, ww, h);

		btnLoagin.setBounds(x0 + wwww, y0 + hhh + hh + h - 4, 60, 30);

		jp.add(txtName);
		jp.add(txtPassword);
		txtPassword.setEchoChar('★');
		txtPassword.setForeground(Color.BLUE);

		jp.add(btnLoagin);

		this.add(jp);
		btnLoagin.addActionListener(this);// 注册事件的监听

		btnLoagin.addMouseListener(this);// 注册鼠标事件的监听

		/* 用户类别单选按钮 */
		String[] strSex = { "", "","" };
		rdbSex = new JRadioButton[strSex.length];
		ButtonGroup bg = new ButtonGroup();// 按钮组（和单选有关）
		for (int i = 0; i < strSex.length; i++) {
			rdbSex[i] = new JRadioButton(strSex[i]);
			bg.add(rdbSex[i]);
			jp.add(rdbSex[i]);
			rdbSex[i].setBounds(x0 + www * i, y0 + hhh + hh + h, 20, 20);
		}
		rdbSex[0].setSelected(true);// 被选择

		// 设置图片的位置
		lblPicture = new JLabel("<html>　　系<br>　　统<br>　　图<br>　　片<br></html>");
		// lblPicture.setBorder(new LineBorder(Color.blue, 5, true));
		lblPicture.setBounds(0, 0, width - 5, height - 30);// 设置图片位置
		displayPictur displayPictur = new displayPictur(0, lblPicture);
		jp.add(lblPicture);

		this.setSize(width, height);
		// 设置窗口关闭事件
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new loginForm();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnLoagin) {
			if (validateForm() == 0) {
				// 学生信息主界面窗口
				new StudentInfoForm(Student);
				this.dispose();// 释放本窗口资源
			} else if (validateForm() == 1) {
				// 学生管理系统主界面窗口
				new StudentMainForm(User);
				this.dispose();// 释放本窗口资源
			} else {
				return;
			}
		} else {
			System.exit(0);// 释放系统所有资源
		}
	}

	/**
	 * 判断是否为空
	 * @return
	 */
	private int validateForm() {
		String name = txtName.getText().trim();// trim()可去除用户名两端空格
		String password = txtPassword.getText();// 划线代表不推荐使用
		if ("".equals(name)) {
			JOptionPane.showMessageDialog(this, "【登录名】不能为空！");
			txtName.requestFocus(true); // 获得焦点(光标转移到该处)
			return -1;
		}
		if ("".equals(password)) {
			JOptionPane.showMessageDialog(this, "【密码】不能为空！");
			txtPassword.requestFocus(true);
			return -1;
		}

		return checkUser(name, password);

	}

	/**
	 * 判断登陆用户类别
	 * 
	 * return 0表示学生，1表示管理员
	 */
	private int checkUser(String name, String password) {

		if (rdbSex[0].isSelected()) {
			StudentSqlDao uDao = new StudentSqlDao();
			ArrayList<Student> al = uDao.getStudent("where binary name='"
					+ name + "'");// binary是二进制转换用来区分大小写
			if (al.size() == 0) {// 登录名不存在
				JOptionPane.showMessageDialog(this, "对不起，【用户名】不存在！");
				txtName.requestFocus(true);
				return -1;
			} else {
				Student = al.get(0);
				if (!Student.getPassword().equals(password)) {
					JOptionPane.showMessageDialog(this, "对不起，【密码】错误！");
					txtPassword.requestFocus(true);
					return -1;
				}
			}
			return 0;

		} else if (rdbSex[1].isSelected()) {
			UserSqlDao uDao = new UserSqlDao();
			ArrayList<User> al = uDao.getUser("where binary name='" + name
					+ "'");// binary是二进制转换用来区分大小写
			if (al.size() == 0) {// 登录名不存在
				JOptionPane.showMessageDialog(this, "对不起，【用户名】不存在！");
				txtName.requestFocus(true);
				return -1;
			} else {
				User = al.get(0);
				if (!User.getPassword().equals(password)) {
					JOptionPane.showMessageDialog(this, "对不起，【密码】错误！");
					txtPassword.requestFocus(true);
					return -1;
				}
			}
			return 1;
		}
		return -1;
	}

	// 鼠标点击事件
	public void mouseClicked(MouseEvent e) {

	}

	// 鼠标进入事件
	public void mouseEntered(MouseEvent e) {
		if (e.getSource() == btnLoagin) {
			btnLoagin.setForeground(Color.red);
		}
	}

	// 鼠标离开事件
	public void mouseExited(MouseEvent e) {
		if (e.getSource() == btnLoagin) {
			btnLoagin.setForeground(null);
		}
	}

	// 鼠标按下事件（左键，右键）
	public void mousePressed(MouseEvent e) {
		((JComponent) e.getSource()).setForeground(Color.blue);
	}

	// 鼠标按键释放事件
	public void mouseReleased(MouseEvent e) {
		((JComponent) e.getSource()).setForeground(Color.yellow);
	}

}
